#include <iostream>
#include <algorithm>
using namespace std;
constexpr int MAXN = 20;
int P, Q, A[MAXN]; // A保存输入数据, 下标从1开始
int dp[MAXN + 1][MAXN + 1]; // dp[i][j] = 释放(i, j)所需的金币
void solve() {
    A[0] = 0;
    A[Q + 1] = P + 1;

    for (int q = 0; q <= Q; q++) {
        dp[q][q + 1] = 0;
    }

    for (int w = 2; w <= Q + 1; w++) {
        for (int i = 0; i + w <= Q + 1; i++) {
            int j = i + w, t = INT_MAX;
            for (int k = i + 1; k < j; k++) {
                t = min(t, dp[i][k] + dp[k][j]);
            }
            dp[i][j] = t + A[j] - A[i] - 2;
        }
    }
    cout << dp[0][Q + 1];
}